package br.urca.controller.webservice;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.inject.Inject;

import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.view.Results;
import br.urca.dao.DisciplinaDAO;
import br.urca.dao.FuncionarioDAO;
import br.urca.dao.OfertaDAO;
import br.urca.model.Disciplina;
import br.urca.model.Funcionario;
import br.urca.model.Oferta;

@Controller
public class WSProfessorController {

	private DisciplinaDAO mDisciplinaDAO;
	private OfertaDAO mOfertaDAO;
	private Result result; 
	private FuncionarioDAO mFuncionarioDAO;
	
	public WSProfessorController() { }
	
	@Inject
	public WSProfessorController(FuncionarioDAO mFuncionarioDAO,  
			DisciplinaDAO mDisciplinaDAO, OfertaDAO mOfertaDAO, 
			Result result) {
		this.mDisciplinaDAO = mDisciplinaDAO;
		this.mOfertaDAO = mOfertaDAO;
		this.result = result;
		this.mFuncionarioDAO = mFuncionarioDAO;
	}
	
	@Get("/testeFuncionarios/json")
	public void teste() {
		List<Funcionario> mList = mFuncionarioDAO.todos();
		this.result.use(Results.json()).from(mList, "resposta").recursive().serialize();
	}
	
	@Get("/loginprofessor/{matricula},{senha}")
	public void loginProfessor(String matricula, String senha) {
		if ((matricula != null) && (senha != null)) {
			Properties properties = new Properties();
			String mMatricula = null;
			String mSenha = null;
			
			try {
				properties.load(new StringReader(matricula));
				mMatricula = properties.getProperty("matricula");
				
				properties.load(new StringReader(senha));
				mSenha = properties.getProperty("senha");
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			if ((mMatricula != null) && (mSenha != null)) {
				Funcionario funcionario = mFuncionarioDAO.buscarPorMatricula(mMatricula);
					if (funcionario != null) {
						if (!funcionario.getSenhaMD5().equalsIgnoreCase(mSenha)) {
							funcionario = null;
						}
					}
				if (funcionario != null) {
					this.result.use(Results.json()).from(funcionario,"resposta")
						.include("setor")
						.include("setor.id")
						.include("cargo")
						.serialize();
				} else {
					this.result.use(Results.json()).from(new Object(), "resposta").serialize();
				}
			} else {
				this.result.use(Results.json()).from(new Object(), "resposta").serialize();
			}
		} else {
			this.result.use(Results.json()).from(new Object(), "resposta").serialize();
		}
	}
	
	@Get("/disciplinasvinculadas/{departamento}")
	public void getDisciplinasVinculadas(String departamento) {
		if ((departamento != null)) {
			Properties properties = new Properties();
			String mCodDepartamento = null;
			
			List<Disciplina> mListDisciplinas = new ArrayList<Disciplina>();
			
			try {
				properties.load(new StringReader(departamento));
				mCodDepartamento = properties.getProperty("departamento");
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			if ((mCodDepartamento != null)) {
				mListDisciplinas.addAll(mDisciplinaDAO.buscarPorDepartamento(mCodDepartamento));
				
				if (!mListDisciplinas.isEmpty()) {
					this.result.use(Results.json()).from(mListDisciplinas, "resposta").serialize();
				} else {
					this.result.use(Results.json()).from(new ArrayList<>(), "resposta").serialize();
				}
			}
		}
	}
	
	@Get("/ofertapordisiciplina/{oferta},{disciplina}")
	public void getOfertasPorDisciplina(String oferta, String disciplina) {
		if ((oferta != null) && (disciplina != null)) {
			
			Properties properties = new Properties();
			String mOferta = null;
			String mDisciplina = null;
			
			List<Oferta> mListOfertas = new ArrayList<Oferta>();
			
			try {
				properties.load(new StringReader(oferta));
				mOferta = properties.getProperty("oferta");
				
				properties.load(new StringReader(disciplina));
				mDisciplina = properties.getProperty("disciplina");
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			if ((mOferta != null) && (mDisciplina != null)) {
				mListOfertas.addAll(mOfertaDAO.buscarPorOfertaDisciplina(mOferta, mDisciplina));
				
				if (!mListOfertas.isEmpty()) {
					this.result.use(Results.json()).from(mListOfertas, "resposta").include("id").serialize();
				} else {
					this.result.use(Results.json()).from(new ArrayList<>(), "resposta").serialize();
				}
			}
		}
	}
	
}
